var t = getApp()
Page({
  data: {
    site:'',
    store:'',
    userinfo:wx.getStorageSync('userinfo'),
    navHeight:t.globalData.navHeight,
    info:'',
    setInter:'',
    getInfo:'',
    buttonStaus:false,
    //券
    showCoupon:false,
    coupon_id:'',
    coupon_name:'',
    type1:false, //是否选择券
    type2:false, //是否使用储值会员
    type3:false, //是否使用差额补费
    pay_type:'', //1
  },
  onLoad: function (options) {
    console.log(options);
    var that = this
    if (options) {
      this.setData({
        site:options.site,
        store:options.store
      })
    }else{
      wx.switchTab({
        url: '/pages/index/index',
      })
    }
    wx.login({
      success: res => {
        t.request('/login',{code:res.code},function(r){
          if(r.result.userInfo.user_id > 0){
            wx.setStorageSync('userinfo',r.result.userInfo);
            that.setData({
              userinfo:r.result.userInfo
            })
            that.get_info_fun();
            that.data.getInfo = setInterval(() => {
              setTimeout(that.get_info_fun, 0);
            }, 6000)
          }
        })
      }
    })
  },
  onShow(e){
    var that = this
  },
  onReady(){
    console.log('onReady')
    setTimeout(this.setButton,30000);
  },
  // 切换优惠券(显示)
  checkDouyin(){
    this.setData({
      showCoupon:!this.data.showCoupon
    })
  },
  // 切换优惠券
  checkCoupon(e){
    this.setData({
      showCoupon:false,
      coupon_id:e.currentTarget.dataset.id,
      coupon_name:e.currentTarget.dataset.name,
    })
    this.get_info_fun();
  },
  // 优惠劵、支付方式
  checkboxChange(e){
    let type = e.currentTarget.dataset.type
    let status = e.currentTarget.dataset.status
    //券
    if(type==1){
      this.setData({
        type1:status==1,
      })
    }
    // 会员
    if(type==2){
      this.setData({
        type2:status==1,
        type3:(this.data.type3==true && status==1)?false:this.data.type3
      })
    }
     // 差价
     if(type==3){
      this.setData({
        type3:status==1,
        type2:(this.data.type2==true && status==1)?false:this.data.type2
      })
    }
    this.get_info_fun();
  },
  //30秒隐藏按钮
  setButton(){
    console.log('buttonStaus')
    clearInterval(this.data.getInfo)
    this.setData({
      buttonStaus:true
    })
  },
  // 核算接口
  get_info_fun(e){
    var that = this
    if (!wx.getStorageSync('userinfo')) {
      that.login = that.selectComponent("#login")
      t.show_login()
      return
    }else{
      let pay_type = '' //
      let type1=that.data.type1,type2=that.data.type2,type3=that.data.type3
      // 只用余额结算
      if(type1==false && type2==true && type3==false){
        pay_type = 1
      }
      // 只选择优惠劵
      if(type1==true && type2==false && type3==false){
        pay_type = 2
      }
       // 只使用补费
       if(type1==false && type2==false && type3==true){
        pay_type = 3
      }
      // 优惠券+余额
      if(type1==true && type2==true && type3==false){
        pay_type = 4
      }
      // 优惠券+差额补费
      if(type1==true && type2==false && type3==true){
        pay_type = 5
      }

      var o = {
        user_id:wx.getStorageSync('userinfo').user_id,
        pay_type:pay_type,
        store:that.data.store,
        site:that.data.site,
        coupon_id:that.data.coupon_id
      }
      t.request('/settle/leaveDataNew',o,function(e){
        console.log(e);
        if (e.code==200) {
          // pay_type 状态回显
        let type1= false,type2= false,type3= false;
        switch(e.result.default_type){
          case 1://只使用余额结算
            type1 = false
            type2 = true
            type3 = false
          break
          case 2://只使用优惠券
            type1 = true
            type2 = false
            type3 = false
          break
          case 3://只使用差额结算
            type1 = false
            type2 = false
            type3 = true
          break
          case 4://优惠券+余额
            type1 = true
            type2 = true
            type3 = false
          break
          case 5://优惠券+余额
            type1 = true
            type2 = false
            type3 = true
          break
        }
          that.setData({
            info:e.result,
            pay_type:e.result.default_type,
            coupon_id:e.result.default_coupon,
            coupon_name:e.result.default_coupon_name,
            type1,
            type2,
            type3,
          })
        }else if(e.code==400){ //错误情况下 返回首页-wlqiao-2024年8月1日21:51:04
          clearInterval(that.data.getInfo);
          wx.showModal({
            title: e.msg,
            showCancel:false,
            success(res){
              wx.switchTab({
                url: '/pages/index/index',
              })
            }
          })
        }
      })
    }
  },
  onPullDownRefresh: function () {
    // wx.showToast({
    //   title: '正在刷新数据...',
    //   icon: 'loading',
    //   duration: 2000
    // });
    // this.onLoad()
    // wx.stopPullDownRefresh();//停止刷新操作
  },
  // 结算离场
  leave(e){
    console.log(e)
    var that = this
    // if (parseFloat(this.data.info.cost)>parseFloat(this.data.info.balance)) {
    //   wx.showModal({
    //     showCancel:false,
    //     title:'提示',
    //     content:'余额不足请充值'
    //   })
    //   return
    // }

    let obj={
      user_id:wx.getStorageSync('userinfo').user_id,
      pay_type:that.data.pay_type,
      store:that.data.store,
      site:that.data.site,
      coupon_id:that.data.coupon_id
    }
    t.request('/settle/leaveSiteNew',obj,function(e){
      if (e.code==200) {
        console.log(e)
        // 微信支付
        if(e.result.WeChat_pay==1){
          var params = e.result.params
          wx.requestPayment({
            nonceStr: params.nonceStr,
            package: params.package,
            paySign: params.paySign,
            signType: 'MD5',
            timeStamp: params.timeStamp,
            'success'(e){
              console.log(e);
              wx.hideLoading({success: (res) => {},})
              // 结算成功获取开门状态
              clearInterval(that.data.getInfo)
              that.data.setInter = setInterval(() => {
                setTimeout(that.get_status, 0);
              }, 1000)
            },
            'fail'(e){
              wx.hideLoading({
                success: (res) => {},
              })
              wx.showModal({
                title: '您取消了支付！',
                showCancel:false,
                complete:function(){
                }
              })
            },
          }) 
        }else{
          // 正常结算
          wx.showToast({
            title: '结算成功',
          })
          clearInterval(that.data.getInfo)
          that.data.setInter = setInterval(() => {
            setTimeout(that.get_status, 0);
          }, 1000)
        }
      }
    })
  },
  getPhoneNumber(e){
    const that = this,
      iv = e.detail.iv,
      da = e.detail.encryptedData,
      carnum = ''
    console.log(e);
    if (!e.detail.encryptedData) return wx.showToast({ title: '授权失败,请重新授权！',icon:'none'})
    wx.checkSession({
      success: (res) => {
        wx.login({
          success(e){
            console.log(e);
            wx.showLoading({
              title: '请稍后',
            })
            t.request('/users/get_session',{code: e.code,iv:iv,da:da},function(event){
              wx.hideLoading({})
              if (event.code!==200) return wx.showToast({ title: event.msg,icon:'none' })
              if (event.code==200) {
                console.log(event.result.phoneNumber)
                wx.setStorageSync('tel', event.result.phoneNumber)
                that.setData({
                  tel:event.result.phoneNumber,
                })
                that.leave()
              }
            })
          }
        })
      },
    })
  },
  // 充值按钮
  invest(e){
    var that = this
    wx.navigateTo({
      url: '/pages/personCenter/chooseStore/invest/invest?store_id=' + that.data.info.store_id + '&card_id='+that.data.info.card_id,
    })
  },
  get_status(e){
    var that = this
    t.request('/settle/open_status',{site:this.data.site},function(e){
      if (e.code==200) {
        wx.showToast({
          title: '开门成功',
          duration:2000,
        })
        clearInterval(that.data.setInter)
        wx.switchTab({
          url: '/pages/index/index',
        })
      }else{
        wx.showLoading({
          title: '正在开门请稍后……',
          duration:3000,
        })
      }
    })
  },
  pay_once(e){
    const that = this
    wx.showLoading({ title: '加载中' })
    t.request('/settle/pay_once',{site:that.data.site},function(e){
      if (e.code!==200) return wx.showToast({ title:e.msg,icon:'none' })
      console.log(e);
      var params = e.result.params
      wx.requestPayment({
        nonceStr: params.nonceStr,
        package: params.package,
        paySign: params.paySign,
        signType: 'MD5',
        timeStamp: params.timeStamp,
        'success'(e){
          wx.hideLoading({ success: (res) => {} })
          clearInterval(that.data.getInfo)
          wx.showToast({
            title: '支付成功',
          })
          wx.switchTab({
            url: '/pages/index/index',
          })
        },
        'fail'(e){
          wx.hideLoading({
            success: (res) => {},
          })
          wx.showModal({
            title: '您取消了支付！',
            showCancel:false,
            complete:function(){
            }
          })
        },
      })
    })
  }
})